package com.mvc.finance.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.mvc.finance.model.BankStatementCharge;
import com.mvc.framework.model.Auditable;
import com.mvc.framework.service.BaseService;

@Service
public class BankStatementChargeManager extends BaseService<BankStatementCharge, Integer> {
	 
	public List<BankStatementCharge> getBankStatementChargesByBankStatementId(Integer bankStatementId, Integer type) {
		BankStatementCharge example = new BankStatementCharge();
		example.setBankStatementId(bankStatementId);
		example.setType(type);
		example.setRecStatus(Auditable.REC_STATUS_VALID);
		return getHibernateTemplate().findByExample(example);
	}

	public void deleteByBankStatementId(int bankStatementId) {
		deleteByWhere("bankStatementId = " + bankStatementId);
	}

	public void saveBankStatementItem(BankStatementCharge orderItem, String operation) throws Exception {
		if (operation.equals("del")) {
			super.delete(orderItem.getId());
		} else if (operation.equals("add")) {
			orderItem.setId(null);
			super.save(orderItem);
		} else if (operation.equals("edit")) {
			super.save(orderItem);
		}
	}

	public void updateBankStatementChargeId(Integer oldOrderId, Integer newOrderId) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createQuery("update " + BankStatementCharge.class.getName() + " set bankStatementId = "
		        + newOrderId + " where bankStatementId = " + oldOrderId);
		query.executeUpdate();
	}

}
